package com.fub.view.mapcomponents;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Typeface;

import com.fub.guidance.Point;
import com.fub.guidance.Vertex;
import com.fub.util.Util;
import com.fub.util.PreferenceDAO;

public class SmallLabelComponent {

	private static final int BACKGROUND_PADDING = 2;
	private static final int TEXT_SIZE = 13;
	private static final int PADDING = 5;
	private Paint paintTextBold;
	private Paint paintToolTip;
	private Vertex node;
	private int tooltipWidth;
	private int tooltipHeight;
	private Point zoomedPosition;
	private int color;
	private Matrix matrix;
	private PreferenceDAO preferenceDAO;

	public SmallLabelComponent(Vertex node, Bitmap icon,
			PreferenceDAO preferenceDao, int color) {
		this.preferenceDAO = preferenceDao;
		this.node = node;
		this.color = color;
		this.matrix = new Matrix();
		initPaints();
	}

	public Vertex getNode() {
		return this.node;
	}

	public boolean isTapped(Point eventPosition) {
		int left = this.zoomedPosition.getX()
				- this.preferenceDAO.getLeftUpperCorner().getX();
		int right = this.zoomedPosition.getX()
				- this.preferenceDAO.getLeftUpperCorner().getX()
				+ this.tooltipWidth;
		int top = this.zoomedPosition.getY()
				- this.preferenceDAO.getLeftUpperCorner().getY()
				- this.tooltipHeight;
		int bottom = this.zoomedPosition.getY()
				- this.preferenceDAO.getLeftUpperCorner().getY();
		Rect bounds = new Rect(left, top, right, bottom);
		return bounds.contains(eventPosition.getX(), eventPosition.getY());
	}

	private void initPaints() {
		// label
		this.paintTextBold = new Paint();
		this.paintTextBold.setAntiAlias(true);
		this.paintTextBold.setTypeface(Typeface.DEFAULT_BOLD);
		this.paintTextBold.setStyle(Paint.Style.FILL);
		this.paintTextBold.setTextSize(TEXT_SIZE);
		this.paintTextBold.setColor(Color.WHITE);
		// background of the tooltip
		this.paintToolTip = new Paint(Paint.ANTI_ALIAS_FLAG);
		this.paintToolTip.setColor(this.color);
		this.paintToolTip.setStrokeWidth(1);
		this.paintToolTip.setShadowLayer(2, 5, 15, Color.argb(25, 0, 0, 0));

	}

	public void draw(Canvas canvas) {
		this.zoomedPosition = Util.calculateZoomedPosition(
				this.node.getPosition(), this.preferenceDAO);
		int textWidth = Util.getTextWidth(this.node.getLabel(),
				this.paintTextBold);
		this.tooltipWidth = textWidth + 2 * PADDING;
		this.tooltipHeight = TEXT_SIZE + PADDING;
		this.zoomedPosition.move(-this.tooltipWidth / 2,
				-this.tooltipHeight / 2);
		Path backgroundPath = getSmallTooltipBackgroundPath(0, 0,
				this.tooltipWidth, this.tooltipHeight);
		this.matrix.postTranslate(this.zoomedPosition.getX(),
				this.zoomedPosition.getY() - this.tooltipHeight);
		backgroundPath.transform(this.matrix);
		canvas.drawPath(backgroundPath, this.paintToolTip);
		Path textPath = getTextPath();
		textPath.transform(this.matrix);
		canvas.drawTextOnPath(this.node.getLabel(), textPath, PADDING,
				-PADDING, this.paintTextBold);
	}

	private Path getTextPath() {
		Path p = new Path();
		p.moveTo(0, this.tooltipHeight);
		p.lineTo(this.tooltipWidth, this.tooltipHeight);
		return p;
	}

	private Path getSmallTooltipBackgroundPath(int offsetX, int offsetY,
			int tooltipWidth, int tooltipHeight) {
		Path p = new Path();
		p.moveTo(offsetX + BACKGROUND_PADDING, offsetY);
		p.lineTo(offsetX + tooltipWidth - BACKGROUND_PADDING, offsetY);
		p.lineTo(offsetX + tooltipWidth, offsetY + BACKGROUND_PADDING);
		p.lineTo(offsetX + tooltipWidth, offsetY + tooltipHeight
				- BACKGROUND_PADDING);
		p.lineTo(offsetX + tooltipWidth - BACKGROUND_PADDING, offsetY
				+ tooltipHeight);
		p.lineTo(offsetX + tooltipWidth / 2 + 5, offsetY + tooltipHeight);
		p.lineTo(offsetX + tooltipWidth / 2, offsetY + tooltipHeight + 5);
		p.lineTo(offsetX + tooltipWidth / 2 - 5, offsetY + tooltipHeight);
		p.lineTo(offsetX + BACKGROUND_PADDING, offsetY + tooltipHeight);
		p.lineTo(offsetX, offsetY - BACKGROUND_PADDING + tooltipHeight);
		p.lineTo(offsetX, offsetY + BACKGROUND_PADDING);
		p.lineTo(offsetX + BACKGROUND_PADDING, offsetY);
		return p;
	}
}
